package com.turing.data.utils;

import org.apache.poi.xwpf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XwpfTUtil {

    /**
     * 替换段落里面的变量
     *
     * @param doc    要替换的文档
     * @param params 参数
     */
    public static void replaceInPara1(XWPFDocument doc, Map<String, String> params) {
        Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
        XWPFParagraph para;
        while (iterator.hasNext()) {
            para = iterator.next();
            replaceInPara(para, params);
        }
    }

//    /**
//     * 替换段落里面的变量
//     *
//     * @param para   要替换的段落
//     * @param params 参数
//     */
//    public static void replaceInPara(XWPFParagraph para, Map<String, String> params) {
//        List<XWPFRun> runs;
//
//        Matcher matcher;
//        if (matcher(para.getParagraphText()).find()) {
//            runs = para.getRuns();
//            int start = -1;
//            int end = -1;
//            String str = "";
//            for (int i = 0; i < runs.size(); i++) {
//                XWPFRun xwpfRun = runs.get(i);
//                String text = xwpfRun.getText(xwpfRun.getTextPosition());
//                if(!text.contains("${")){
//                    text = "${"+text+"}";
//                }
//                System.out.println(text);
//                if (text == null || text.trim().equals("")) {
//                    continue;
//                }
//                //替换
//                for (Map.Entry<String, String> entry : params.entrySet()) {
//                    String key = entry.getKey();
//                    String value = entry.getValue();
//                    if (text.contains(key)) {
//                        text = text.replace(key, value);
//                        xwpfRun.setText(text, 0);
//                        break;
//                    }else  if(text.contains("${")){
//                        text = text.replace("${","");
//                    }else if(text.contains("}")){
//                        text = text.replace("}","");
//                    }
//                }
//            }
//        }
//
//
//    }

    /**
     * 替换段落里面的变量
     *
     * @param para   要替换的段落
     * @param params 参数
     */
    public static void replaceInPara(XWPFParagraph para, Map<String, String> params) {
        List<XWPFRun> runs;

        if (matcher(para.getParagraphText()).find()) {
            runs = para.getRuns();
            int start = -1;
            int end = -1;
            String str = "";
            for (int i = 0; i < runs.size(); i++) {
                XWPFRun xwpfRun = runs.get(i);
                String text = xwpfRun.getText(xwpfRun.getTextPosition());
                System.out.println(text);
                if (text == null || text.trim().equals("")) {
                    continue;
                }
                if(!text.contains("${")|| !text.contains("}")){
                    text = "${" + text + "}";
                }
                //替换
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (text.contains(key)) {
                        text = text.replace(key, value);
                        xwpfRun.setText(text, 0);
                        break;
                    }
                }
                if(text.contains("${")||text.contains("}")){
                    text = " ";
                    xwpfRun.setText(text, 0);
                }
            }

//            for (int i = start; i <= end; i++) {
//                para.removeRun(i);
//                i--;
//                end--;
//            }
//
//            for (String key : params.keySet()) {
//                if (str.equals(key)) {
//                    String param = String.valueOf(params.get(key));
//                    if (param.indexOf("\n")>-1) {
//                        String[] texts = param.split("\n");
//                        for (int i = 0; i < texts.length; i++) {
//                            para.createRun().setText(texts[i]);
//                            para.createRun().addCarriageReturn();
//                        }
//                    } else {
//                        para.createRun().setText(param);
//                    }
//                }
//            }


        }
    }

    /**
     * 替换表格里面的变量
     *
     * @param doc    要替换的文档
     * @param params 参数
     */
    public static void replaceInTable(XWPFDocument doc, Map<String, String> params) {
        Iterator<XWPFTable> iterator = doc.getTablesIterator();
        XWPFTable table;
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        List<XWPFParagraph> paras;
        while (iterator.hasNext()) {
            table = iterator.next();
            rows = table.getRows();
            for (XWPFTableRow row : rows) {
                cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    paras = cell.getParagraphs();
                    for (XWPFParagraph para : paras) {
                        replaceInPara(para, params);
                    }
                }
            }
        }
    }

    /**
     * 正则匹配字符串
     *
     * @param str
     * @return
     */
    private static Matcher matcher(String str) {
        Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher;
    }

    /**
     * 关闭输入流
     *
     * @param is
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭输出流
     *
     * @param os
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
